]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
fileUtils: Handle symlinks when moving/deleting dirs
authorFlorian Müllner <fmuellner@gnome.org>
Fri, 23 May 2025 20:34:14 +0000 (22:34 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Sat, 24 May 2025 13:47:07 +0000 (15:47 +0200)
We can handle them like regular files for the operations in question,
but we have to do so explicitly. Otherwise the link will not be
moved or removed, and in the latter case prevent the parent
directory from being removed as well (as it ends up non-empty).

Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/8417
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3721>

js/misc/fileUtils.js
tests/unit/fileUtils.js

index cbbeac85e46b0732e4a6eca0a922033a15157042..216fa3e8379e7c7a613737b43d43e38246933243 100644 (file)
@@ -52,7 +52,7 @@ export function recursivelyDeleteDir(dir, deleteParent) {
     while ((info = children.next_file(null)) != null) {
         let type = info.get_file_type();
         let child = dir.get_child(info.get_name());
-        if (type === Gio.FileType.REGULAR)
+        if (type === Gio.FileType.REGULAR || type === Gio.FileType.SYMBOLIC_LINK)
             child.delete(null);
         else if (type === Gio.FileType.DIRECTORY)
             recursivelyDeleteDir(child, true);
@@ -78,7 +78,7 @@ export function recursivelyMoveDir(srcDir, destDir) {
         let type = info.get_file_type();
         let srcChild = srcDir.get_child(info.get_name());
         let destChild = destDir.get_child(info.get_name());
-        if (type === Gio.FileType.REGULAR)
+        if (type === Gio.FileType.REGULAR || type === Gio.FileType.SYMBOLIC_LINK)
             srcChild.move(destChild, Gio.FileCopyFlags.NONE, null, null);
         else if (type === Gio.FileType.DIRECTORY)
             recursivelyMoveDir(srcChild, destChild);
index 47d88145422827f35ac1aba55fbb9b5e59fa9012..5aba0457bd9598683b4af559394a78fa8e424531 100644 (file)
@@ -77,6 +77,16 @@ describe('recursivelyDeleteDir()', () => {
         expect(() => recursivelyDeleteDir(dir, true)).not.toThrow();
         expect(dir.query_exists(null)).toBeFalse();
     });
+
+    it('handles symlinks', () => {
+        touchFile(dir, 'file');
+
+        const link = dir.get_child('link');
+        link.make_symbolic_link('file', null);
+
+        expect(() => recursivelyDeleteDir(dir, true)).not.toThrow();
+        expect(dir.query_exists(null)).toBeFalse();
+    });
 });
 
 describe('recursivelyMoveDir()', () => {
@@ -123,4 +133,15 @@ describe('recursivelyMoveDir()', () => {
         expect(dstSubdir.query_exists(null)).toBeTrue();
         expect(dstSubdir.get_child('file').query_exists(null)).toBeTrue();
     });
+
+    it('handles symlinks', () => {
+        touchFile(srcDir, 'file');
+
+        const link = srcDir.get_child('link');
+        link.make_symbolic_link('file', null);
+
+        expect(() => recursivelyMoveDir(srcDir, dstDir)).not.toThrow();
+        expect(dstDir.query_exists(null)).toBeTrue();
+        expect(dstDir.get_child('link').query_exists(null)).toBeTrue();
+    });
 });